package com.neusoft.weekreport.component.controller;

import com.neusoft.weekreport.component.bizService.RoleRefMenuBizService;
import com.neusoft.weekreport.component.req.RoleRefMenuAddReq;
import com.neusoft.weekreport.component.req.RoleRefMenuByRoleIdReq;
import com.neusoft.weekreport.component.resp.GetMenuInfoListResp;
import com.neusoft.weekreport.component.resp.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@EnableTransactionManagement
@RequestMapping(value = "roleMenu")
public class RoleRefMenuController {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    RoleRefMenuBizService roleRefMenuBizService;
    @RequestMapping(value = "/roleRfMenu",method = RequestMethod.GET)
    public ResponseBean<GetMenuInfoListResp> menuInfoListByRoleId(RoleRefMenuByRoleIdReq req, HttpServletRequest request){
        ResponseBean<GetMenuInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetMenuInfoListResp resp = roleRefMenuBizService.menuInfoListByRoleId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询菜单信息成功",resp);


        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //添加权限与菜单关联
    @RequestMapping(value = "/roleRfMenu",method = RequestMethod.POST)
    public ResponseBean addRoleRefMenuByRoleIdAndMenuId(@RequestBody RoleRefMenuAddReq req,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean<>();
        try{
             roleRefMenuBizService.addRoleRefMenuByRoleIdAndMenuId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"添加菜单权限关联成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }
}
